﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public  class cgthckBLL
    {
        private cgthckMasterTableAdapter _cgthckMasterlAdapter = null;
        private cgthckMasterTableAdapter cgthckMasterAdapter
        {
            get
            {
                if (_cgthckMasterlAdapter == null) _cgthckMasterlAdapter = new cgthckMasterTableAdapter();
                return _cgthckMasterlAdapter;

            }
        }
        private cgthckDetailTableAdapter _cgthckDetailAdapter = null;
        private cgthckDetailTableAdapter cgthckDetailAdapter
        {
            get
            {
                if (_cgthckDetailAdapter == null) _cgthckDetailAdapter = new cgthckDetailTableAdapter();
                return _cgthckDetailAdapter;

            }
        }
        /// <summary>
        /// 查询采购退货主表
        /// </summary>
        /// <returns></returns>

        public purchase.cgthckMasterDataTable getcgthckMasterAll()
        {
            try
            {
                return cgthckMasterAdapter.GetDatacgthckMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询退货从表
        /// </summary>
        /// <returns></returns>

        public purchase.cgthckDetailDataTable getcgthckDetailAll()
        {
            try
            {
                return cgthckDetailAdapter.GetDatacgthckDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询退货表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.cgthckMasterDataTable getcgthckMasterAllBybillNo(string billNo)
        {
            try
            {
                return cgthckMasterAdapter.GetDatacgthckMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询采购退货从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.cgthckDetailDataTable getscgthckDetailAllBybillNo(string billNo)
        {
            try
            {
                return cgthckDetailAdapter.GetDatacgthckDetailBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 修改采购退货表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int cgthckInsert(purchase.cgthckMasterDataTable masterTable, purchase.cgthckDetailDataTable pTable, string state, string billNo, string Date)
        {
            cgthckDetailModel cgthckDetailRow = new cgthckDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                cgthckMasterDAL cgthckMaster = new cgthckMasterDAL(tn, conn);
                cgthckDetailDAL cgthckDetail = new cgthckDetailDAL(tn, conn);
                cgthckMaster.cgthckMasterInsertII(masterTable, pTable);
                //basListDAL basList = new basListDAL(tn, conn);
                //if (state == "新增")
                //{

                //    purchase.cgthckMasterDataTable cgddTable = getcgthckMasterAllBybillNo(billNo);
                //    if (cgddTable.Rows.Count == 0)
                //    {
                //        cgthckMaster.cgthckMasterInsert(row);
                //    }
                //    else
                //    {
                //        row.billNo = "R" + Date + "-" + basicSql.getOrderSerail("采购退货单号");
                //        cgthckMaster.cgthckMasterInsert(row);
                //    }

                //    //更新单据编号的过程 
                //    string orderSerial = basicSql.getOrderSerail("采购退货单号");
                //    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "采购退货单号");
                //}
                //if (state == "编辑")
                //{
                //    cgthckMaster.cgthckMasterUpdate(row);
                //}


                //foreach (purchase.cgthckDetailRow detailRow in pTable)
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        cgthckDetailRow.recordid = detailRow.recordid;
                //        cgthckDetailRow.billNo = detailRow.billNo;
                //        cgthckDetailRow.matNum = detailRow.matNum;
                //        cgthckDetailRow.matSpec = detailRow.matSpec;
                //        cgthckDetailRow.acount  = detailRow.acount ;
                //        cgthckDetailRow.matName  = detailRow.matName ;
                //        cgthckDetailRow.qty = detailRow.qty;
                //        cgthckDetailRow.price = detailRow.price;
                //        cgthckDetailRow.currency = detailRow.currency;
                //        cgthckDetailRow.unit = detailRow.unit;
                //        cgthckDetailRow.fqty = detailRow.fqty;
                //        cgthckDetailRow.funit = detailRow.funit;
                //        cgthckDetailRow.createDate = DateTime.Now;
                //        if (!detailRow.IsmarkNull())
                //        {
                //            cgthckDetailRow.mark = detailRow.mark;
                //        }
                //        else
                //        {
                //            cgthckDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        cgthckDetail.cgthckDetailInsert(cgthckDetailRow);


                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        cgthckDetail.cgthckDetailUpdate(cgthckDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        cgthckDetail.cgthckDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行采购退货表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int cgthckMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                cgthckMasterDAL cgthckMaster = new cgthckMasterDAL(tn, conn);
                cgthckMaster.cgthckDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
